using Ln.Fram.Ddd.Application;
using Ln.Rbac.Application.Contracts.Dtos.Dept;
using Ln.Rbac.Application.Contracts.IServices;
using Ln.Rbac.Domain.Entities;
using Ln.Rbac.Domain.Repositories;
using Ln.Rbac.Domain.Shared.Consts;
using SqlSugar;
using Volo.Abp.Application.Dtos;

namespace Ln.Rbac.Application.Services.System;

/// <summary>
/// Dept服务实现
/// </summary>
public class DeptService : LnCrudAppService<DeptAggregateRoot, DeptGetOutputDto, DeptGetListOutputDto, Guid, DeptGetListInputVo, DeptCreateInputVo, DeptUpdateInputVo>, IDeptService
{
	private IDeptRepository _repository;

	public DeptService(IDeptRepository repository) : base(repository)
	{
		_repository = repository;
	}

	[RemoteService(false)]
	public async Task<List<Guid>> GetChildListAsync(Guid deptId)
	{
		return await _repository.GetChildListAsync(deptId);
	}

	/// <summary>
	/// 通过角色id查询该角色全部部门
	/// </summary>
	/// <returns></returns>
	//[Route("{roleId}")]
	public async Task<List<DeptGetListOutputDto>> GetRoleIdAsync(Guid roleId)
	{
		var entities = await _repository.GetListRoleIdAsync(roleId);
		return await MapToGetListOutputDtosAsync(entities);
	}

	/// <summary>
	/// 多查
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	public override async Task<PagedResultDto<DeptGetListOutputDto>> GetListAsync(DeptGetListInputVo input)
	{
		RefAsync<int> total = 0;
		var entities = await _repository._DbQueryable
			.WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
			.WhereIF(input.State is not null, u => u.State == input.State)
			.OrderBy(u => u.OrderNum, OrderByType.Asc)
			.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
		return new PagedResultDto<DeptGetListOutputDto>
		{
			Items = await MapToGetListOutputDtosAsync(entities),
			TotalCount = total
		};
	}

	protected override async Task CheckCreateInputDtoAsync(DeptCreateInputVo input)
	{
		var isExist = await _repository.IsAnyAsync(x => x.DeptCode == input.DeptCode);
		if (isExist)
			throw new UserFriendlyException(DeptConst.Exist);
	}

	protected override async Task CheckUpdateInputDtoAsync(DeptAggregateRoot entity, DeptUpdateInputVo input)
	{
		var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id).AnyAsync(x => x.DeptCode == input.DeptCode);
		if (isExist)
			throw new UserFriendlyException(DeptConst.Exist);
	}
}